import { Controller } from "../../mvc/core/Controller";
import { ITotalLoadReward } from "../../../modules/interfaces/ITotalLoadReward";
import { IView } from "../../mvc/interfaces/IView";
import { TotalLoadRewardModule } from "../../../modules/TotalLoadRewardModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { SLookupPlayerReply } from "../../../Message/Game/SLookupPlayerReply";
import { DataEvent } from "../events/DataEvent";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { GameProxy } from "../mvc/GameProxy";
import { Cache } from "../cache/Cache";
import { EToplistType } from "../../../Message/Public/EToplistType";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { TotalLoadRewardConfig } from "../resource/TotalLoadRewardConfig";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
type int = number;
//class TotalLoadRewardController
    
    export  class TotalLoadRewardController extends Controller
    {
       

        public get totalLoadReward(): ITotalLoadReward
        {
            if (null == this._totalLoadReward) 
            {
                this.initView();
            }
            return this._totalLoadReward;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (this._totalLoadReward == null) 
            {
                this._totalLoadReward = new TotalLoadRewardModule();
            }
            return this._totalLoadReward;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.OpenTotalLoadModule, this.onOpenTotalLoadModule);
            Dispatcher.addEventListener(EventName.GetTotalLoadReward, this.onGetTotalLoadReward);
            Dispatcher.addEventListener(EventName.getTotalLoadExtraReward, this.onGetTotalLoadExtraReward);
            NetDispatcher.addCmdListener(ServerCommand.TotalLoadRewardComment, this.onTotalLoadRewardComment);
            Dispatcher.addEventListener(EventName.RankGetDataSuccess, this.onRankGetDataSuccess);
            Dispatcher.addEventListener(EventName.CheckTotalLoadRewardRankPlayer, this.onCheckTotalLoadRewardRankPlayer);
            NetDispatcher.addCmdListener(ServerCommand.PlayerInfoReply, this.onPlayerInfoMsg);
            return;
        }

        /* internal  */onPlayerInfoMsg(arg1: Object): void
        {
            var loc1=arg1 as SLookupPlayerReply;
            var loc2=loc1.miniPlayer.entityId.id;
            if (this.isCheckTotalLoadRewardRank) 
            {
                this.isCheckTotalLoadRewardRank = false;
                if (!(this._totalLoadReward == null) && this._toPlayerId == loc2) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TotalLoadRewardRankOnlinePlayerShowMenu));
                }
            }
            return;
        }

        /* internal  */onCheckTotalLoadRewardRankPlayer(arg1: DataEvent): void
        {
            this.isCheckTotalLoadRewardRank = true;
            var loc1=arg1.data as SEntityId;
            this._toPlayerId = loc1.id;
            GameProxy.playerInfoProxy.lookupRequest_async(Cache.instance.role.entityInfo.entityId, loc1, null, false);
            return;
        }

        /* internal  */onRankGetDataSuccess(arg1: DataEvent): void
        {
            var loc1=cache.ranking.getDataByPage(EToplistType._EToplistTypeLoadDays, 1, 0);
            Cache.instance.totalLoadReward.rankArr = loc1;
            if (this._totalLoadReward && !this._totalLoadReward.isHide) 
            {
                this._totalLoadReward.updateRank(loc1);
            }
            return;
        }

        /* internal  */onTotalLoadRewardComment(arg1: SAttribute): void
        {
            var loc1=arg1;
            if (loc1.value > TotalLoadRewardConfig.instance.maxDay) 
            {
                loc1.value = TotalLoadRewardConfig.instance.maxDay;
            }
            Cache.instance.totalLoadReward.sattribute = loc1;
            var loc2=arg1.valueStr.split(",");
            if (arg1.value == 1 || arg1.value == 0) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShineTotalLoadRewardLogo, false));
            }
            else if (!(loc2[0] === "") || loc2[1] == 1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShineTotalLoadRewardLogo, true));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShineTotalLoadRewardLogo, false));
            }
            if (this._totalLoadReward && !this._totalLoadReward.isHide) 
            {
                this._totalLoadReward.updateInfo(Cache.instance.totalLoadReward.sattribute);
                this._totalLoadReward.updateRank(Cache.instance.totalLoadReward.rankArr);
            }
            return;
        }

        /* internal  */onGetTotalLoadExtraReward(arg1: DataEvent): void
        {
            GameProxy.roleProxy.getTotalLoadExtraReward();
            return;
        }

        /* internal  */onGetTotalLoadReward(arg1: DataEvent): void
        {
            GameProxy.roleProxy.getTotalLoadReward();
            return;
        }

        /* internal  */onOpenTotalLoadModule(arg1: DataEvent): void
        {
            if (Cache.instance.totalLoadReward.sattribute.value == 1 || Cache.instance.totalLoadReward.sattribute.value == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70570));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.RankingTypeChange, EToplistType._EToplistTypeLoadDays));
            if (this.totalLoadReward.isHide) 
            {
                this.totalLoadReward.updateInfo(Cache.instance.totalLoadReward.sattribute);
                this.totalLoadReward.updateRank(Cache.instance.totalLoadReward.rankArr);
                this.view.show();
            }
            else 
            {
                this._totalLoadReward.hide();
            }
            return;
        }

        private /* var */_totalLoadReward: ITotalLoadReward;

        private /* var */isCheckTotalLoadRewardRank: Boolean;

        private /* var */_toPlayerId: int;
    }
